package com.yaorange.nongjt.certificate.controller.wxapi;


import com.yaorange.nongjt.certificate.bo.LoginUserBo;
import com.yaorange.nongjt.certificate.bo.RegisterBo;
import com.yaorange.nongjt.certificate.domain.NjtUserPerfect;
import com.yaorange.nongjt.certificate.service.impl.CommonService;
import com.yaorange.nongjt.certificate.service.INjtUserRegisterService;
import com.yaorange.nongjt.common.constant.Constants;
import com.yaorange.nongjt.common.core.domain.AjaxResult;
import com.yaorange.nongjt.common.core.domain.model.LoginBody;
import com.yaorange.nongjt.common.utils.nongjt.SmsUtils;
import com.yaorange.nongjt.common.utils.nongjt.ValidateCodeUtils;
import com.yaorange.nongjt.framework.web.service.SysLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @author coach tam
 * @email 327395128@qq.com
 * @values 坚持灵活 灵活坚持
 * @since 2024/10/15
 */
@RestController
@RequestMapping("/api/user")
public class NjtUserAPI {
    //spring data redis
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private SmsUtils smsUtils;

    @Autowired
    private INjtUserRegisterService njtUserRegisterService;

    @Autowired
    private CommonService commonService;
    @GetMapping("/sendValidateCode")
    public AjaxResult sendValidateCode(@RequestParam("mobilePhone") String mobilePhone) {
        //判断手机号1分钟内是否已经发送过
        Boolean hasKey = stringRedisTemplate.opsForValue().setIfAbsent("yzm:limit:" + mobilePhone, "123456", 1,TimeUnit.MINUTES);
        if (!Boolean.TRUE.equals(hasKey)) {
            return AjaxResult.error("验证码1分钟只能发送一次");
        }
        String data = ValidateCodeUtils.generateValidateCode(6);
        //验证码保存到redis,过期时间5分钟
        stringRedisTemplate.opsForValue().set("yzm:store:" + mobilePhone, data, 5, TimeUnit.MINUTES);
        //阿里云短信平台发送6位验证码
        smsUtils.sendSms(mobilePhone, data);
        return new AjaxResult(200, "发送成功",data);
    }

//    http://localhost:8080/api/user/checkMobilePhone?mobilePhone=15883783522
     @GetMapping("/checkMobilePhone")
    public AjaxResult checkMobilePhone(@RequestParam("mobilePhone") String mobilePhone) {
        //判断手机号是否已经注册
        Boolean hasKey = njtUserRegisterService.checkMobilePhone(mobilePhone);
        if (Boolean.TRUE.equals(hasKey)) {
            return AjaxResult.error("手机号已注册");
        }
        return AjaxResult.success();
    }

    //http://localhost:8080/api/user/register
    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBo registerBo) {

        njtUserRegisterService.register(registerBo);
        return AjaxResult.success();
    }

    @Autowired
    private SysLoginService loginService;

    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.appLogin(loginBody.getUsername(), loginBody.getPassword());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @GetMapping("/queryLoginUser")
    public AjaxResult queryLoginUser() {
        LoginUserBo loginUserBo = commonService.getLoginUser();
        return AjaxResult.success(loginUserBo);
    }


    @PostMapping("/perfect")
    public AjaxResult perfect(@RequestBody NjtUserPerfect perfect) {
        njtUserRegisterService.perfect(perfect);
        return AjaxResult.success();
    }
}
